package ch.zhaw.cctd.gui;

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JWindow;

/**
 * Ermöglicht die Optionen-Übersicht und das Verlassen des Spiels.
 * 
 * @author Lukas Spirig
 */
public class MenuGUI extends JWindow implements ActionListener {

	private static final long serialVersionUID = -6820969027307918833L;
	private CCTDFrame frame;
	private JButton back = new JButton("Zurück");
	private JButton leave = new JButton("Abmelden");
	private JButton quit = new JButton("Beenden");

	public MenuGUI(CCTDFrame frame) {
		super(frame);
		this.frame = frame;
		Container c = this.getContentPane();
		c.setLayout(new GridLayout(3,1));

		back.addActionListener(this);
		leave.addActionListener(this);
		quit.addActionListener(this);
		this.setAlwaysOnTop(true);

		c.add(back);
		c.add(leave);
		c.add(quit);

		setSize(200, 200);
		setVisible(false);
	}

	/**
	 * This method is called when a user presses the ESC Key. It makes the Menu window appear in the center of the parent Frame.
	 */
	public void display() {
		frame.setEnabled(false);
		setLocation(frame.getLocation().x + frame.getWidth()/2-this.getWidth()/2,frame.getLocation().y + frame.getHeight()/2-this.getWidth()/2);
		setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(back)) {
			frame.setEnabled(true);
			this.setVisible(false);
		} else if (e.getSource().equals(leave)) {
			frame.setEnabled(true);
			this.setVisible(false);
			frame.leaveGame();
			frame.start();
		} else if (e.getSource().equals(quit)) {
			frame.quitGame();
		}
	}

}
